#!/bin/zsh

export SLAPD_EXECUTABLE=/usr/sbin/slapd
export OPENLDAP_INSTALLED=${OPENLDAP_INSTALLED:-0}
export LDAP_RUNTIME_DIR=${LDAP_RUNTIME_DIR:-"/var/lib/openldap/run"}
export LDAP_CONFIG_DIR=${LDAP_CONFIG_DIR:-"/etc/openldap/slapd.d"}
export LDAP_LOG_DIR=/var/log/openldap
export LDAP_LOG_FILE=${LDAP_LOG_DIR}/slapd.log
export LDAP_ERROR_LOG_FILE=${LDAP_LOG_DIR}/error.log
export LDAP_LOG_LEVEL=${LDAP_LOG_LEVEL:-"64"}
export LDAP_DOMAIN=${LDAP_DOMAIN-"domain"}
export LDAP_SUBDOMAIN=${LDAP_SUBDOMAIN:-"openldap"}
export LDAP_USER=${LDAP_USER:-"ldap"}
export LDAP_GROUP=${LDAP_GROUP:-"ldap"}
export LDAP_URL=${LDAP_URL:-"ldap:///"}
export LDAP_ROOT_DN=${LDAP_ROOT_DN:-"cn=root,dc=${LDAP_SUBDOMAIN},dc=${LDAP_DOMAIN}"}
export LDAP_ROOT_PASSWORD_INPUT=${LDAP_ROOT_PASSWORD_INPUT:="123321"}
# LDAP_ROOT_PASSWORD=`slappasswd -s ${LDAP_ROOT_PASSWORD_INPUT}`
export LDAP_ROOT_PASSWORD=${LDAP_ROOT_PASSWORD_INPUT}
# unset LDAP_ROOT_PASSWORD_INPUT
#

ok () {
	echo "[ "${GREEN_START}"OK"${GREEN_STOP}" ]"
}

fail () {
	echo "[ "${RED_START}"FAILED"${RED_STOP}" ]"
}

function config_slapd () {
	install -m 755 -o ldap -g ldap -d ${LDAP_CONFIG_DIR}
	install -m 755 -o ldap -g ldap -d ${LDAP_RUNTIME_DIR}
	install -m 755 -o ldap -g ldap -d ${LDAP_LOG_DIR}
	if [ -f /etc/conf.d/slapd ]; then
		# Open up /etc/conf.d/slapd in your favorite editor
		# Comment out cfgfile="/etc/openldap/slapd.conf"
		sed -i 's,^cfgfile=,#cfgfile=,g' /etc/conf.d/slapd 
		# Uncomment cfgdir="/etc/openldap/slapd.d"
		sed -i 's,^#cfgdir=.*$,cfgdir="/etc/openldap/slapd.d",g' /etc/conf.d/slapd 
	fi
	[ -f /etc/openldap/slapd.conf ] && rm /etc/openldap/slapd.conf
}

function prepare_slapd_ldif () {
	# Open up /etc/openldap/slapd.ldif in your favorite editor. 
	# Search for the file names ending with .la and change the extension to .so
	sed 's/([a-zA-Z0-9]*).la/\1.so/g' /etc/openldap/slapd.ldif > /tmp/slapd.ldif
	diff /etc/openldap/slapd.ldif /tmp/slapd.ldif > /dev/null 

	# Edit slapd.ldif again.
	# echo "LDAP_SUBDOMAIN = ${LDAP_SUBDOMAIN}"
	# echo "LDAP_DOMAIN = ${LDAP_DOMAIN}"
	# echo "Before Replace"
	# sed -n '/^olcSuffix:/p' /etc/openldap/slapd.ldif 
	# sed -n '/^olcRootDN: cn=\(.*\),dc=\(.*\),dc=\(.*\)/p' /etc/openldap/slapd.ldif 
	# Find the olcSuffix: keyword.
	# Change the value to match your domain
	sed -i "s/^olcSuffix: dc=\(.*\),dc=\(.*\)$/olcSuffix: dc="${LDAP_SUBDOMAIN}",dc="${LDAP_DOMAIN}"/g" /etc/openldap/slapd.ldif 
	# Find olcRootDN:
	# Change the value to match your domain
	sed -i "s/^olcRootDN: cn=\(.*\),dc=\(.*\),dc=\(.*\)$/olcRootDN: "${LDAP_ROOT_DN}"/g" /etc/openldap/slapd.ldif 
	sed -i "s/^olcRootPW: secret/olcRootPW: "${LDAP_ROOT_PASSWORD}"/g" /etc/openldap/slapd.ldif 
	# echo "After Replace"
	# sed -n '/^olcSuffix:/p' /etc/openldap/slapd.ldif 
	# sed -n '/^olcRootDN: cn=\(.*\),dc=\(.*\),dc=\(.*\)/p' /etc/openldap/slapd.ldif 

	# Edit slapd.ldif again.
	# Find the line include: file:///etc/openldap/schema/core.ldif
	# Append similar lines referencing cosine.ldif, inetorgperson.ldif, and nis.ldif.
}

function import_slapd_ldif () {
	# Importing ldif
	if [ -f ${LDAP_CONFIG_DIR}/cn=config.ldif ]; then
		rm -f ${LDAP_CONFIG_DIR}/cn=config.ldif 2>&1 >> ${LDAP_LOG_FILE}
	fi
	if [ -d ${LDAP_CONFIG_DIR}/cn=config ]; then
		rm -rf ${LDAP_CONFIG_DIR}/cn=config/ 2>&1 >> ${LDAP_LOG_FILE}
	fi
	if [ -f /etc/openldap/slapd.ldif ]; then
		/usr/sbin/slapadd -n 0 -F ${LDAP_CONFIG_DIR} -l /etc/openldap/slapd.ldif 2>&1 >> ${LDAP_LOG_FILE} 2> ${LDAP_ERROR_LOG_FILE}
	fi
	chown -R ldap:ldap ${LDAP_CONFIG_DIR}/*
}

function install_openldap () {
	echo -n "install service openldap: "
	config_slapd

	prepare_slapd_ldif

	import_slapd_ldif

	OPENLDAP_INSTALLED=1
	ok
	return true;
}

function is_openldap_installed () {
	# echo -n "check if openldap is instanced: "
	if [ ${OPENLDAP_INSTALLED} -eq 0 ]; then
		# fail
		return false;
	else
		# ok
		return true;
	fi
}

function start_openldap() {
	if is_openldap_installed; then
		install_openldap
	fi
	echo -n "start service openldap: "
	PID=`ps | grep ${SLAPD_EXECUTABLE} | sed '/grep/d'`
	if [ -z ${PID} ]; then
		/usr/bin/nohup ${SLAPD_EXECUTABLE} -d ${LDAP_LOG_LEVEL} -u ${LDAP_USER} -g ${LDAP_GROUP} -F ${LDAP_CONFIG_DIR} -h ${LDAP_URL} > ${LDAP_LOG_FILE} 2> ${LDAP_ERROR_LOG_FILE} &
		if [ $? -eq 0 ]; then
			ok
		else
			fail
		fi
	else
		ok
	fi
}

function status_openldap() {
	PID=`ps | grep ${SLAPD_EXECUTABLE} | sed '/grep/d'`
	echo -n "status of service openldap: "
	if [ -z ${PID} ]; then
		fail
		return false;
	else
		ok
		return true;
	fi
}

function show_log_file_writable () {
	if [ -w ${LDAP_LOG_FILE} ]; then
		echo "${LDAP_LOG_FILE} is writable"
	else
		echo "${LDAP_LOG_FILE} is not writable"
	fi
}

function main () {
	action=$1
	case ${action} in
		start)
			start_openldap
			;;
		status)
			status_openldap
			;;
		install)
			install_openldap
			;;
		import)
			import_slapd_ldif
			;;
		*)
	esac
}

touch ${LDAP_LOG_FILE}

main $@

# vim: ts=4 sw=4 ff=unix

